package com.speaktoitcookbook.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.speaktoitcookbook.bean.Collection;
import com.speaktoitcookbook.bean.Cookbook;
import com.speaktoitcookbook.bean.UserInfo;
import com.speaktoitcookbook.dao.ICollectionDao;
import com.speaktoitcookbook.util.DBUtil;
import com.speaktoitcookbook.util.TransformUtil;

/**
 * 用户收藏数据操作类
 * 
 * @author 赛艳志 杨强
 * 
 */
public class CollectionDaoImpl implements ICollectionDao {
    private Connection conn = null;

    public CollectionDaoImpl(Connection conn) {
        this.conn = conn;
    }

    @Override
    public boolean doCreate(Collection collection) throws SQLException {
        boolean result = false;
        String sql = "insert into `tb_collection`(`userID`, `cbID`, `coDate`, `coInfo`) values(?, ?, ?, ?)";

        Object[] params = TransformUtil.beanTOArray(collection);
        params[2] = new Timestamp(collection.getCollectDate().getTime());
        result = DBUtil.execute(conn, sql, params);
        return result;
    }

    @Override
    public boolean doDelete(Collection collection) throws SQLException {
        boolean result = false;
        result = this.doDelete(collection.getUserID(),
                collection.getCookbookID());
        return result;
    }

    @Override
    public boolean doDelete(String userID, long cookbookID) throws SQLException {
        boolean result = false;
        String sql = "delete from `tb_collection` where `userID`= ? and `cbID` = ?";
        result = DBUtil.execute(conn, sql, userID, cookbookID);
        return result;
    }

    @Override
    public boolean doUpdate(Collection collection) throws SQLException {
        boolean result = false;
        String sql = "update `tb_collection` set `userID` = ?, `cbID` = ?, `coInfo` = ? where `userID`= ? and `cbID` = ?";
        int item = 0;
        Object[] params = new Object[3];
        params[item++] = collection.getUserID();
        params[item++] = collection.getCookbookID();
        params[item++] = collection.getCollectInfo();
        params[item++] = collection.getUserID();
        params[item++] = collection.getCookbookID();

        result = DBUtil.execute(conn, sql, params);
        return result;
    }

    @Override
    public List<Collection> getCollection(String userID) throws SQLException {
        List<Collection> result = null;
        String sql = "select * from `tb_collection` where `userID` = ?";
        List<Object[]> all = DBUtil.query(conn, sql, userID);
        if (all.size() > 0) {
            result = new ArrayList<Collection>();
            for (Object[] objects : all) {
                Collection collection = new Collection();
                collection = (Collection) TransformUtil.arrayToBean(objects,
                        Collection.class);
                result.add(collection);
            }
        }
        return result;
    }

    @Override
    public List<Cookbook> getCollections(String userID) throws SQLException {
        List<Cookbook> result = null;
        String sql = "select `tb_cookbook`.* from `tb_collection`, `tb_cookbook` where `tb_collection`.`userID` = ? and `tb_collection`.`cbID`=`tb_cookbook`.`cbID`";
        List<Object[]> all = DBUtil.query(conn, sql, userID);
        if (all.size() > 0) {
            result = new ArrayList<Cookbook>();
            for (Object[] objects : all) {
                Cookbook collection = new Cookbook();
                collection = (Cookbook) TransformUtil.arrayToBean(objects,
                        Cookbook.class);
                result.add(collection);
            }
        }
        return result;
    }

    @Override
    public List<UserInfo> getCollection(long cookbookID) throws SQLException {
        List<UserInfo> result = null;
        String sql = "select `tb_userinfo`.* from `tb_userinfo`, `tb_collection` where `cbID` = ?"
                + " and `tb_userinfo`.`userID` = `tb_collection`.`userID`";
        List<Object[]> all = DBUtil.query(conn, sql, cookbookID);
        if (all.size() > 0) {
            result = new ArrayList<UserInfo>();
            for (Object[] objects : all) {
                UserInfo userInfo = new UserInfo();
                userInfo = (UserInfo) TransformUtil.arrayToBean(objects,
                        UserInfo.class);
                result.add(userInfo);
            }
        }
        return result;
    }

}
